import * as glob from "glob"

import dayjs from "dayjs"
import { desktopPath } from "@/utils"
import { useNode } from "@/hooks"

const { fs, path } = useNode()

const clearHistory = () => {
  const options = { windowsPathsNoEscape: true }

  const pattern = path.join(desktopPath, "history/*/")

  const courses = glob.sync(pattern, options)

  courses.forEach(coursePath => {
    const historyDate = path.basename(coursePath)

    const diffDay = dayjs().diff(historyDate, "day")

    const isExpired = diffDay > 2

    isExpired && fs.rmSync(coursePath, { force: true, recursive: true })
  })
}

export default clearHistory
